package com.rzjm.exercise.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.exercise.common.base.service.impl.BaseServiceImpl;
import com.rzjm.exercise.common.config.RequestDataHelper;
import com.rzjm.exercise.common.utils.DateUtils;
import com.rzjm.exercise.common.utils.MathUtil;
import com.rzjm.exercise.module.dao.DeviceRangeRecordDao;
import com.rzjm.exercise.module.dao.DeviceRangeResultDao;
import com.rzjm.exercise.module.domain.*;
import com.rzjm.exercise.module.service.DeviceRangeRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeviceRangeRecordServiceImpl extends BaseServiceImpl<DeviceRangeRecordDao, DeviceRangeRecord> implements DeviceRangeRecordService {

    @Resource
    private DeviceRangeRecordDao deviceRangeRecordDao;
    @Resource
    private DeviceRangeResultDao deviceRangeResultDao;

    @Override
    public Map<String, Object> getChartData(WxUser wxUser, DeviceRange deviceRange) {
        double maxValue = 0d;
        Map<String,Object> mapReturn = new HashMap<>();

        QueryWrapper<DeviceRangeResult> qwResult = new QueryWrapper<>();
        qwResult.eq("device_id",deviceRange.getId());
        if(wxUser == null || (wxUser !=null && wxUser.getIsTest() == 0)){
            qwResult.eq("is_test", 0);
        }
        qwResult.orderByDesc("create_time");
        qwResult.last("limit 1");
        DeviceRangeResult lastResult = deviceRangeResultDao.selectOne(qwResult);
        if(lastResult != null){

            //判断当前是否需要重置 记录表
            String tableName = lastResult.getRecordTableName();
            // 配置表名
            Map<String,Object> map = new HashMap<>();
            map.put("ck_device_range_record", tableName);
            RequestDataHelper.setRequestData(map);

            DeviceRangeRecord lastRecord = getLastRecordByDeviceId(deviceRange.getDeviceNum(),deviceRange.getDataCode(), tableName);
            if(lastRecord!= null){
                double maxAngle = MathUtil.getAngleByBevel(lastRecord.getNowHeight(), deviceRange.getStoreDiameter());
                List<String> times = new ArrayList<>();
                List<String> values = new ArrayList<>();
                QueryWrapper<DeviceRangeRecord> qw = new QueryWrapper<>();
                qw.eq("device_id",lastResult.getDeviceId());
                qw.eq("data_code",lastResult.getDataCode());
                qw.gt("now_angle", maxAngle);
                qw.orderByAsc("now_angle");

                System.out.println("deviceRange.getCylinderHeight():" + deviceRange.getCylinderHeight());
                List<DeviceRangeRecord> rangeRecords = deviceRangeRecordDao.selectList(qw);
                for (DeviceRangeRecord rangeRecord : rangeRecords){
                    System.out.println("rangeRecord.getNowHeight(): " + rangeRecord.getNowHeight());
                    times.add(rangeRecord.getNowAngle() + "");
                    double dd = MathUtil.getDouble((deviceRange.getCylinderHeight() -  rangeRecord.getNowHeight()));
                    values.add(dd + "");
                }

                mapReturn.put("times",times);
                mapReturn.put("values",values);
                mapReturn.put("count",times.size());
                mapReturn.put("maxValue",maxValue);
                mapReturn.put("minValue",0);
            }
        }


        return mapReturn;
    }


    /**
     *  获取 最后一条 的记录
     * @param deviceNum
     * @param dataCode
     * @return
     */
    public DeviceRangeRecord getLastRecordByDeviceId(String deviceNum, String dataCode, String tableName) {

        // 配置表名
        Map<String,Object> map = new HashMap<>();
        map.put("ck_device_range_record", tableName);
        RequestDataHelper.setRequestData(map);

        QueryWrapper<DeviceRangeRecord> qw = new QueryWrapper<>();
        qw.eq("device_num", deviceNum);
        qw.eq("data_code", dataCode);
        qw.orderByDesc("now_angle");
        qw.last("limit 1");
        DeviceRangeRecord record = deviceRangeRecordDao.selectOne(qw);
        return record;
    }


}
